<?php

class Application_Model_Gps extends Zend_Db_Table_Abstract
{
    protected $_name = 'gps';
    //извлекаем все точки с уникальным imei
    /*public function getAllPoints($id_imei) {
        $this->_name = 'gps';
        $select = $this->select()->from($this->_name)
                ->where('id_imei = ?', intval($id_imei));
        return $this->fetchAll($select)->toArray();
    }*/
    
    public function getAllPoints($id_imei) {
        $this->_name = 'gps';
        $this->db = Zend_Db_Table::getDefaultAdapter();
        $role = $this->db->query("SELECT id_imei, latitude, longitude, battery,speed, date 
                                  FROM gps
                                  WHERE id_imei = $id_imei
                                  ORDER BY id DESC");
        return $role;
    }
    
    public function getRoutePoints($id_route) {
        $this->_name = 'gps';
        $this->db = Zend_Db_Table::getDefaultAdapter();
        $role = $this->db->query("SELECT id_imei, latitude, longitude, battery,speed, date 
                                  FROM gps
                                  WHERE id_route = $id_route
                                  ORDER BY id DESC");
        return $role;
    }
    
    public function saveGpsData($data) {
        $this->_name = 'gps';
        $this->insert($data);
    }
    /*
    public function getLastPoint($imei){
        $select = $this->select()->from($this->_name)
                ->where('imei = ?', $imei)
                ->order('id DESC')
                ->limit(1);
        return $this->fetchRow()->toArray();
    }
    */
    public function getLastPoint($id_imei) {
        $this->db = Zend_Db_Table::getDefaultAdapter();
        $role = $this->db->query("SELECT id_imei, latitude, longitude, battery,speed, date 
                                  FROM gps
                                  WHERE id_imei = $id_imei
                                  ORDER BY id DESC
                                  LIMIT 1");
        return $role;
    }
    
    public function getTwoLastPoints($id_imei) {
        $this->db = Zend_Db_Table::getDefaultAdapter();
        $role = $this->db->query("SELECT id_imei, latitude, longitude, battery,speed, date 
                                  FROM gps
                                  WHERE id_imei = $id_imei
                                  ORDER BY id DESC
                                  LIMIT 2");
        return $role;
    }
    
    //добавить новое устройство
    public function addImei($data) {
        $this->_name = 'imei';
        $this->insert($data);
        return $this->getAdapter()->lastInsertId();
    }
    
    //выборка указанного устройства
    public function getSelectImei($id){
        $this->_name = 'imei';
        $select = $this->select()
                       ->from($this->_name)
                       ->where('id =?', intval($id));
        return $this->fetchRow($select);
    }
    
    //получить все устройства для главной
    public function getAllImei($id_user){
        $this->_name = 'imei';
        $select = $this->select()
                       ->from($this->_name)
                       ->where('id_user =?', intval($id_user));
        return $this->fetchAll($select)->toArray();                       
    }

    //создание нового маршрута
    public function newRoute($data) {
        $this->_name = 'gps_routes';
        $this->insert($data);
    }
    
    //получить id выбранного устройства
    public function getImeiId($imei) {
        $this->_name = 'imei';
        $select = $this->select()
                       ->from($this->_name)
                       ->where('imei =?', $imei);
        return $this->fetchRow($select);
    }
    
    //получить последний маршрут
    public function getLastRoute($id_imei) {
        $this->_name = 'gps_routes';
        $select = $this->select()
                       ->from($this->_name)
                       ->where('id_imei =?', intval($id_imei));
        return $this->fetchRow($select);
    }
    
    public function getAllCategories() {
        $this->_name = 'category_transport';
        $select = $this->select()
                       ->from($this->_name);
        return $this->fetchAll($select)->toArray();
    }
    
    public function getLastImei($id_user) {
        $this->_name = 'imei';
        $select = $this->select()
                       ->from($this->_name)
                       ->where('id_user =?', intval($id_user));
        return $this->fetchRow($select);
    }
    
    public function getAllRoutes($id_imei) {
        $this->_name = 'gps_routes';
        $select = $this->select()
                       ->from($this->_name)
                       ->where('id_imei =?', $id_imei);
        return $this->fetchAll($select)->toArray();
    }   
}


